﻿//时间格式化
Date.prototype.format = function (format) //author: meizz 
{
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(),    //day 
        "h+": this.getHours(),   //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter 
        "S": this.getMilliseconds() //millisecond 
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

function StrToDate(JSONDateString) {
    var date = new Date(parseInt(JSONDateString.replace("/Date(", "").replace(")/", ""), 10));
    return date.format("yyyy-MM-dd hh:mm:ss");
}

//删除事件
function delClick(obj) {
    if (confirm("确认要删除？")) {
        var delid = $(obj).attr("id");
        $.ajax({
            type: "post",
            url: "/ashx/Inwrl.ashx",
            data: { delbarid: $(obj).attr("alt") },
            success: function (msg) {
                //alert('删除成功');
            }
        });
        $(obj).parent().parent().remove();
    }
}

//修改事件 条形码重复
function updateClick(obj) {

    var barid = $(obj).attr("alt");
    $.ajax({
        type: "post",
        url: "/ashx/Inwrl.ashx",
        data: { "barid": barid },
        success: function (msg) {
            var barobj = eval('(' + msg + ')');

            $("#txtgc").val(barobj.goodscode);
            $("#txtgn").val(barobj.goodsname);
            $("#txtgwt").val(barobj.gwt);
            $("#txttare1").val(barobj.tare1);
            $("#txttare2").val(barobj.tare2);
            $("#txtnwt").html(barobj.nwt);
            $("#txtqty").val(barobj.qty);
            $("#txtsid").val(barobj.stroageNo);
            $("#txtHw").val(barobj.rubbishcode);
            $("#lblHwType").html(barobj.rubbishtype);
            $("#txtdno").val(barobj.rdh);
            $("#txtpn").val(barobj.packageno);
            $("#lblInDate").html(StrToDate(barobj.createdate));
            $("#txtremark").val(barobj.remark);
            $("#lblbarcode").html(barobj.barcodevalue);
            $(".HidPostion").val(barobj.storageid);
        }
    });

}

$(document).ready(function () {

    //如果非移库称重，隐藏机械误差
    if ($(".HidOutid").val() == "0") {
        $("#labjcwc").hide();
        $("#spanMec").hide();
    }

    //物品编码选取
    $("#txtgc").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ashx/Autocomplete.ashx",
                data: { "goods": $("#txtgc").val(), "type": "DH" },
                dataType: "json",
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.label + " " + item.value,
                            value: item.value,
                            lab: item.label,
                            goodsid: item.goodsid
                        }
                    }));
                },
                error: function (result, status) {
                    alert('请输入正确的物品编码');
                }
            })

        },
        select: function (event, ui) {
            $("#txtgc").val(ui.item.lab);
            $("#txtgn").val(ui.item.value);
            $("#hidgdid").val(ui.item.goodsid);
            return false;
        },

        minLength: "1"
    });


    //库位下拉选择根据处理地做筛选
    //2013.06.06  RedPlum
    $("#txtsid").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ashx/BasicStorage.ashx",
                data: {
                    "word": $("#txtsid").val()
                             , "graphic": escape($(".hidOutDepart").val())
                             , "belong": escape($(".hidInDepart").val())
                },
                dataType: "json",
                success: function (data) {
                    response(data);
                },
                error: function (result, status) {
                    alert('请输入正确的库位');
                }
            })

        },
        select: function (event, ui) {
            $("#txtsid").val(ui.item.label);
            $(".HidPostion").val(ui.item.value);
            return false;
        },

        minLength: "1"
    });


    //危废下拉选择
    $("#txtHw").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/ashx/Autocomplete.ashx",
                data: { "goods": $("#txtHw").val(), "type": "hw" },
                dataType: "json",
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.label + " " + item.value,
                            value: item.value,
                            lab: item.label
                        }
                    }));
                },
                error: function (result, status) {
                    alert('请输入正确的危废编码');
                }
            })

        },
        select: function (event, ui) {
            $("#txtHw").val(ui.item.lab);
            $("#lblHwType").html(ui.item.value);
            $("#hidHwid").val(ui.item.goodsid);
            return false;
        },

        minLength: "1"
    });


    //净重计算

    $("#txttare2,#txtgwt,#txttare1").blur(function () {
        if ($("#txttare1").val() != "" && $("#txtgwt").val() != "" && $("#txttare2").val() != "") {
            var gwt = parseFloat($("#txtgwt").val());
            var t1 = parseFloat($("#txttare1").val());
            var t2 = parseFloat($("#txttare2").val());
            var nwt = (gwt - t1 - t2).toFixed(2);
            $("#txtnwt").html(nwt);
        }
    });

    //为空验证
    function CheckInput() {
        var strErr = "";
        if ($("#txtgc").val() == "") strErr += "物品编码不能为空！\n";
        if ($("#txtgn").val() == "") strErr += "物品名称不能为空！\n";
        if ($("#txtgwt").val() == "" || isNaN($("#txtgwt").val())) strErr += "毛重必须输入数字！\n";
        if ($("#txtqty").val() == "" || isNaN($("#txtqty").val())) strErr += "数量必须输入数字！\n";
        if ($("#txttare1").val() == "" || isNaN($("#txttare1").val())) strErr += "皮重1必须输入数字！\n";
        if ($("#txttare2").val() == "" || isNaN($("#txttare2").val())) strErr += "皮重2必须输入数字！\n";
        //if ($("#txtHw").val() == "") strErr += "废物编码不能为空！\n";
        if ($("#txtsid").val() == "" || $(".HidPostion").val() == "0" || $(".HidPostion").val() == "") strErr += "必须选择合法库位！\n";
        return strErr;
    }

    var timer = null; function do_click(event) {
        clearTimeout(timer); // 这里加一句是为了兼容 Gecko 的浏览器 /
        if (event.detail == 2)
            return;
        // 同上句的作用 
        timer = setTimeout(function () {
            // click 事件的处理 
        }, 300);
    }



    $("#btnSaveBarcode").dblclick()
    {
        clearTimeout(timer);
    }

    //插入操作
    $("#btnSaveBarcode").click(function () {
        InsertBar();
    });

    function InsertBar() {
        //插入验证
        var strErr = CheckInput();
        if (strErr != "") {
            $("#txtgc").focus();
            alert(strErr);
            return;
        } else {
            checkGoodsName();
        }
    }

    //        function getIsLock(parisLock) {
    //            if (parisLock) {
    //                return "锁定";
    //            }
    //            else {
    //                return "未锁定";
    //            }
    //        }



    //插入条形码事件
    function InsertBarCode() {

        var flag = checkweight(0);
        if (flag == -1 && $(".HidOutid").val() != "0") {
            alert($("#hidOutid").val());
            alert('入库单所有重量加上机械误差大于出库单重量，请重新输入机械误差');
            $(".txtMechanerr").val("");
            $(".txtMechanerr").focus();
            return;
        }
        var jston = { "loadnumid": $(".hiddenloadid").val(),
            "goodscode": $("#txtgc").val(),
            "goodsname": $("#txtgn").val(),
            "barcode": $("#lblbarcode").html(),
            "gwt": $("#txtgwt").val(),
            "nwt": $("#txtnwt").html(),
            "tare1": $("#txttare1").val(),
            "tare2": $("#txttare2").val(),
            "qty": $("#txtqty").val(),
            "packageno": $("#txtpn").val(),
            "storageid": $(".HidPostion").val(),
            "storageno": $("#txtsid").val(),
            "parid": $(".hiddenParSid").val(),
            "hidInStoID": $(".hiddenInSid").val(),
            "status": $(".HidStatus").val(),
            "wrlno": $(".txtin").val(),
            "oper": $(".hiddenOper").val(),
            "rcode": $("#txtHw").val(),
            "rname": $("#lblHwType").html(),
            "plateid": $(".dropPlate").val(),
            "plateno": $(".dropPlate").find("option:selected").text(), //选中的文本值
            "remark": $("#txtremark").val(),
            "islocak": $("#chkIsLock").attr("checked"),
            "alllock": $(".alllock").attr("checked"),
            "boxamount": $("#boxamount").val(),
            "cjoid": $(".hiddencjoid").val(),
            "rdh": $("#txtdno").val(),
            "moveoutid": $(".hidOutid").val(),
            "inDepart": $(".hidInDepart").val(),
            "outDepart": $(".hidOutDepart").val(),
            "month": $(".dropYear").val() + $(".dropMonth").val(),
            "goodsid": $("#hidgdid").val(),
            "intype": "M",
            "jxwc": $(".txtMechanerr").val(),
            "insid": $(".hiddenInSid").val()
        };

        $.ajax({
            type: "post",
            url: "/ashx/Inwrl.ashx",
            data: jston,
            dataType: "json",
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            },
            success: function (msg) {
                //var obj =  msg ;
                $(".labInDate").html(StrToDate(msg[1].instoragetime));
                $(".txtin").val(msg[1].wrlno);
                var tr = "<tr>";
                tr += "<td>" + ($('table tr').length - 3) + "</td>";
                tr += "<td class='barcodevalue'>" + msg[0].goodscode + "</td>";
                tr += "<td>" + msg[0].goodsname + "</td>";
                tr += "<td>" + msg[0].gwt + "</td>";
                tr += "<td>" + msg[0].tare1 + "</td>";
                tr += "<td>" + msg[0].tare2 + "</td>";
                tr += "<td class='barnwt'>" + msg[0].nwt + "</td>";
                tr += "<td>" + msg[0].qty + "</td>";
                tr += "<td>" + msg[0].stroageNo + "</td>";
                tr += "<td>" + msg[0].rubbishcode + "</td>"; //危废编码
                tr += "<td>" + msg[0].rubbishtype + "</td>"; //危废类型
                tr += "<td>" + msg[0].rdh + "</td>"; //联单号
                tr += "<td>" + msg[0].packageno + "</td>"; //箱号
                tr += "<td>" + StrToDate(msg[1].instoragetime) + "</td>";
                tr += "<td>" + msg[0].remark + "</td>"; //备注
                tr += "<td>" + msg[0].barcodevalue + "</td>";   //条码
                tr += "<td>" + msg[0].plateno + "</td>"; //车牌号
                tr += "<td><input type='checkbox' class='flase' title='" + msg[0].id + "' " + GetIsLock(msg[0].isLocak) + "  />锁定</td>"; //锁定
                tr += "<td><img src='/images/jian.png' alt='" + msg[0].id + "' width='16' onclick='javascript:delClick(this)'  height='17''/></td>";
                tr += "<td><img src='/images/icons/button_update.gif' alt='" + msg[0].id + "' onclick='javascript:updateClick(this)' height='17' class='update'/></td>";
                tr += "<td><a href='/Storage/BarCodeWRL.aspx?barCodeID=" + msg[0].id + "' target='_blank'>条码重打</a></td>";
                tr += "</tr>";

                //清空
                $("#txtgc").val("");
                $("#txtgn").val("");
                //                $("#dropt").html("");
                $("#txtgwt").val("0");
                $("#txtnwt").html("0");
                $("#txttare1").val("0");
                $("#txttare2").val("0");
                $("#txtqty").val("");
                $("#txtpn").val("");
                $("#HidPostion").val("");
                $("#txtsid").val("");
                $("#txtHw").val("");
                $(".hiddenInSid").val(msg[1].id);
                //                        $(".hidInDepart").val(obj[1].InDepart);
                //                        $(".hidOutDepart").val(obj[1].OutDepart);
                $("#lblHwType").html("");
                $("#txtremark").val("");
                $("#txtdno").val("");
                $(".HidStatus").val($(".HidStatus").val() + 1);
                $("#lblbarcode").html("");
                $("#chkIsLock").attr("checked", false);
                //打印大标签
                window.open("BarCodeWRL.aspx?barcodeid=" + msg[0].id, "_blank");
                if (msg[0] != null) {
                    $("#tab").append(tr);
                }
            }
        });
    }


    //2013-02-27 梁玲
    //验证称重页面的物品编码，废物编码，库位
    function checkGoodsName() {
        $.ajax({
            type: "post",
            url: "/ashx/IsGoodCode.ashx?",
            data: { goodCodeid: $("#txtgc").val(), isHw: $("#txtHw").val(), sid: $("#txtsid").val(), checkStatus: "1" },
            success: function (msg) {
                switch (msg) {
                    case "-1":
                        alert("不能成功调用！");
                        break;
                    case "0":
                        InsertBarCode();
                        break;
                    case "1":
                        alert("物品编码不合法！");
                        break;
                    case "2":
                        alert("废物编码不合法！");
                        break;
                    case "3":
                        alert("库位编码不合法！");
                        break;
                    default:
                        alert("不正常数据！");
                        break;
                }
            }
        });
    }

    //2013-03-05 梁玲
    //判断是否选中
    function GetIsLock(islock) {
        if (islock == true)
            return "checked";
        else
            return "";
    }

    //2013-03-05 梁玲
    //checkbox绑定点击事件
    $(".flase").each(function () {
        $(this).click(function () {
            var id = $(this).attr("title");
            var isLock = "0";
            if ($(this).attr("checked") == "checked")
                isLock = "1";
            else
                isLock = "0";
            updataLock(id, isLock);
        });

    });

    function updataLock(id, Islock) {
        $.ajax({
            type: "post",
            url: "/ashx/Inwrl.ashx",
            data: { barcodeid: id, islock: Islock },
            success: function (msg) {
                alert(msg);
            }
        });
    }


});

function changemecweight() {
    if ($("#btnMec").val() == "编辑") {
        $(".txtMechanerr").removeAttr("disabled");
        $("#btnCancel").removeAttr("disabled");
        var mecnwt = 0.00;
        mecnwt = parseFloat($('.txtMechanerr').val());
        $("#hidMec").val(mecnwt);
        $("#btnMec").val("保存");
    }
    else {
        var wrlid = $(".hiddenInSid").val();
        if (checkweight(1) == -1) {
            alert('入库单所有重量加上机械误差大于出库单重量，请重新输入机械误差');
            $(".txtMechanerr").val("");
            $(".txtMechanerr").focus();
        }
        else {
            var jston = { "loadnumid": $(".hiddenloadid").val(),
                "hidInStoID": wrlid,
                "oper": $(".hiddenOper").val(),
                "alllock": $(".alllock").attr("checked"),
                "cjoid": $(".hiddencjoid").val(),
                "moveoutid": $(".hidOutid").val(),
                "inDepart": $(".hidInDepart").val(),
                "outDepart": $(".hidOutDepart").val(),
                "month": $(".dropYear").val() + $(".dropMonth").val(),
                "mec": $(".txtMechanerr").val(),
                "parid": $(".hiddenParSid").val()
            };
            $.ajax({
                type: "post",
                url: "/ashx/Inwrl.ashx",
                data: jston,
                dataType: "json",
                success: function (msg) {
                    $(".txtMechanerr").attr("disabled", "disabled");
                    $("#btnCancel").attr("disabled", "disabled");
                    $("#btnMec").val("编辑");
                    if (wrlid == 0) {
                        $(".hiddenInSid").val(msg.id);
                        $(".txtin").val(msg.wrlno);
                    }
                }
            });
         
        }
    }
}

function checkweight(flag) {
    //出库单重量
    var outsnwt = 0.00;
    outsnwt = parseFloat($('.lblOuttotal').html());
    //已经称的重量
    var insnwt = 0.00;
    insnwt = parseFloat($('.tdinsweight').html());
    //机械误差
    var jxwc = 0.00;
    jxwc = parseFloat($('.txtMechanerr').val());
    if(flag==1){
        if(insnwt+jxwc > outsnwt){
            return -1;
        }
        else{
            return 0;
        }
    }

    //输入的条码净重
    var tnwt = 0.00;
    if ($('#txtnwt').html() != "")
        tnwt = parseFloat($('#txtnwt').html());

    if( $("#btnMec").val()=="保存"){
        alert("请先保存机械误差");
        return;
    }
    
    if(insnwt + jxwc + tnwt > outsnwt){
        return -1;
    }
    else{
        return 0;
    }
}

//放弃净重修改
function cancelnwt() {

    var srcmec = $("#hidMec").val();
    $(".txtMechanerr").val(srcmec);
    $(".txtMechanerr").attr("disabled", "disabled");
    $("#btnCancel").attr("disabled", "disabled");
    $("#btnMec").val("编辑");
}


var rFlag = 1;
$("#txtDiv").hide();

function showRemark() {
    if ($(".hiddenInSid").val() == 0) {
        alert("请先生成入库单");
    }
    else {
        if ($(".insremark").val() == "" || $(".hiddenOper").val() == "admin") {
            $(".insremark").disabled = false;
        }
        else {
            $(".insremark").disabled = true;
            $("#btnSaveRemark").hide();
            $("#btnCancelRemark").hide();

        }
        $("#txtDiv").show();
        $("#imgDiv").hide();
    }
}

$("#btnSaveRemark").click(function () {

    $("#txtDiv").hide();
    $("#imgDiv").show();
    updateRemark();

});


$("#btnCancelRemark").click(function () {

    $("#txtDiv").hide();
    $("#imgDiv").show();
});

function updateRemark() {
    var uname = $(".hiddenOper").val();
    var remark = $(".insremark").val();
    var insid = $(".hiddenInSid").val();
    $.ajax({
        type: "post",
        url: "/ashx/InStorage.ashx",
        data: { uname: uname, insremark: remark, insid: insid },
        success: function (msg) {
            if (msg != "TRUE") {
                alert("更新失败");
            } else {
                if ($(".insremark").val() == "" || $(".hiddenOper").val() == "admin") {
                    //$(".insremark").disabled = false;
                }
                else {
                    $(".insremark").attr("disabled", true);
                    //$(".insremark").disabled = "disabled";
                }
            }
        }
    });
}